 
 
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>滑块文档 - Layui</title>
<meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="apple-mobile-web-app-status-bar-style" content="black"> 
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="format-detection" content="telephone=no">
  <script>
  ;!function(){self!==parent&&(location.href="//www.baidu.com/")}();
  </script>
  <link rel="stylesheet" href="../../layui/dist/css/layui.css" media="all">
  <link rel="stylesheet" href="../../static/css/global.css" media="all">
</head>
<body><div class="layui-header header header-doc" autumn="">
  <div class="layui-container">
    <a class="logo" href="../../index.html">
      <img src="../../static/images/layui/logo.png" alt="layui">
    </a>
    <div class="layui-form layui-hide-xs component" lay-filter="LAY-site-header-component"></div>
    <div class="layui-hide-xs site-notice"></div>
    
    <ul class="layui-nav" id="LAY_NAV_TOP">
      <li class="layui-nav-item layui-this">
        <a href="../index.html">文档</a> 
      </li>
      <li class="layui-nav-item ">
        <a href="../../demo/index.html">示例</a>
      </li>
      
      <li class="layui-nav-item">
        <a href="javascript:;">
          <!--<span class="layui-badge-dot" style="left:0; right: auto; margin: -4px 0 0 5px;"></span>--> 
          周边
        </a>
        <dl class="layui-nav-child layui-nav-child-c">  
          <dd class="layui-hide-sm layui-show-xs" lay-unselect="">
            <a href="javascript:window.open('https://gitee.com/sentsin/layui/issues');" target="_blank" rel="nofollow">问题反馈</a>
            <hr>
          </dd>
          
          <dd lay-unselect=""><a href="../../alone.html" target="_blank" lay-unselect="">独立组件</a></dd>
          <dd lay-unselect=""><a href="../../extend/index.html" target="_blank">扩展组件</a></dd>
        </dl>
      </li>
      
      
    </ul>
  </div>
</div>
 
<!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
<!--[if lt IE 9]>
  <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
  <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
 
<ul class="site-dir">
  <li><a href="#use"><cite>使用</cite></a></li>
  <li><a href="#options"><cite>基础参数</cite></a></li>
  <li>回调函数</li>
  <ul style="margin-left: 15px;">
    <li><a href="#ontips"><cite>自定义提示文本</cite></a></li>
    <li><a href="#change"><cite>数值改变的回调</cite></a></li>
  </ul>
  <li>实例方法</li>
  <ul style="margin-left: 15px;">
    <li><a href="#setValue"><cite>动态改变滑块数值</cite></a></li>
  </ul>
</ul>
<div class="layui-container layui-row">
  
<div class="layui-col-md3">
  <div class="layui-panel site-menu">
    <ul class="layui-menu layui-menu-lg">
      <li class="layui-menu-item-group" lay-options="{type: 'group', isAllowSpread: true}">
        <div class="layui-menu-body-title">
          基础说明
        </div>
        <hr>
        <ul>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../index.html">
                <span>开始使用 </span> 
                <span class="layui-font-12 layui-font-gray">Getting Started</span>
              </a>
              
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../base/infrastructure.html">
                <span>底层方法 </span>
                <span class="layui-font-12 layui-font-gray">基础支撑</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../base/element.html">
                <span>页面元素 </span>
                <span class="layui-font-12 layui-font-gray">规范 公共类 属性</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../base/modules.html">
                <span>模块规范 </span>
                <span class="layui-font-12 layui-font-gray">使用 扩展</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../base/faq.html">
                <span>常见问题 </span>
                <span class="layui-font-12 layui-font-gray">FAQ</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../base/changelog.html">
                <span>更新日志 </span>
                <span class="layui-font-12 layui-font-gray">changelog</span>
                
                  <span class="layui-badge-dot"></span>
                
              </a>
            </div>
          </li>
        </ul>
      </li>
      
      <li class="layui-menu-item-group" lay-options="{type: 'group', isAllowSpread: true}">
        <div class="layui-menu-body-title">
          页面元素
        </div>
        <hr>
        <ul>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../element/layout.html">
                <span>布局 </span>
                <span class="layui-font-12 layui-font-gray">栅格 后台管理布局</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../element/color.html">
                <span>颜色 </span>
                <span class="layui-font-12 layui-font-gray">主色调 颜色搭配</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../element/icon.html">
                <span>图标 </span>
                <span class="layui-font-12 layui-font-gray">iconfont 字体图标</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../element/anim.html">
                <span>动画 </span>
                <span class="layui-font-12 layui-font-gray">内置 CSS3 动画</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../element/button.html">
                <span>按钮 </span>
                <span class="layui-font-12 layui-font-gray">button</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../element/form.html">
                <span>表单 </span>
                <span class="layui-font-12 layui-font-gray">form 元素集合</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../element/nav.html">
                <span>导航 </span>
                <span class="layui-font-12 layui-font-gray">nav 面包屑</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../element/menu.html">
                <span>菜单 </span>
                <span class="layui-font-12 layui-font-gray">menu 基础菜单</span>
                
                  <span class="layui-badge-dot"></span>
                
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../element/tab.html">
                <span>选项卡 </span>
                <span class="layui-font-12 layui-font-gray">Tabs 切换</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../element/progress.html">
                <span>进度条 </span>
                <span class="layui-font-12 layui-font-gray">progress</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../element/panel.html">
                <span>面板 </span>
                <span class="layui-font-12 layui-font-gray">panel 卡片 折叠</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../element/table.html">
                <span>表格 </span>
                <span class="layui-font-12 layui-font-gray">静态 table</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../element/badge.html">
                <span>徽章 </span>
                <span class="layui-font-12 layui-font-gray">小圆点  小边框</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../element/timeline.html">
                <span>时间线 </span>
                <span class="layui-font-12 layui-font-gray">timeline</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="../element/auxiliar.html">
                <span>辅助 </span>
                <span class="layui-font-12 layui-font-gray">引用 字段集 横线等</span>
              </a>
            </div>
          </li>
        </ul>
      </li>
      
      <li class="layui-menu-item-group" lay-options="{type: 'group', isAllowSpread: true}">
        <div class="layui-menu-body-title">
          内置模块
        </div>
        <hr>
        <ul>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="layer.html">
                <span>弹出层 </span>
                <span class="layui-font-12 layui-font-gray">layer</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="laydate.html">
                <span>日期与时间选择 </span>
                <span class="layui-font-12 layui-font-gray">laydate</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="laypage.html">
                <span>分页 </span>
                <span class="layui-font-12 layui-font-gray">laypage</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="laytpl.html">
                <span>模板引擎 </span>
                <span class="layui-font-12 layui-font-gray">laytpl</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="table.html">
                <span>数据表格 </span>
                <span class="layui-font-12 layui-font-gray">table</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="form.html">
                <span>表单 </span>
                <span class="layui-font-12 layui-font-gray">form</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="upload.html">
                <span>文件上传 </span>
                <span class="layui-font-12 layui-font-gray">upload</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="dropdown.html">
                <span>下拉菜单 </span>
                <span class="layui-font-12 layui-font-gray">dropdown</span>
                
                  <span class="layui-badge-dot"></span>
                
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="transfer.html">
                <span>穿梭框 </span>
                <span class="layui-font-12 layui-font-gray">transfer</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="tree.html">
                <span>树形组件 </span>
                <span class="layui-font-12 layui-font-gray">tree</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="colorpicker.html">
                <span>颜色选择器 </span>
                <span class="layui-font-12 layui-font-gray">colorpicker</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="element.html">
                <span>常用元素操作 </span>
                <span class="layui-font-12 layui-font-gray">element</span>
              </a>
            </div>
          </li>
          <li class="layui-menu-item-checked2">
            <div class="layui-menu-body-title">
              <a href="">
                <span>滑块 </span>
                <span class="layui-font-12 layui-font-gray">slider</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="rate.html">
                <span>评分 </span>
                <span class="layui-font-12 layui-font-gray">rate</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="carousel.html">
                <span>轮播 </span>
                <span class="layui-font-12 layui-font-gray">carousel</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="flow.html">
                <span>流加载 </span>
                <span class="layui-font-12 layui-font-gray">flow</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="util.html">
                <span>工具组件 </span>
                <span class="layui-font-12 layui-font-gray">util</span>
              </a>
            </div>
          </li>
          <li class="">
            <div class="layui-menu-body-title">
              <a href="code.html">
                <span>代码高亮显示 </span>
                <span class="layui-font-12 layui-font-gray">code</span>
              </a>
            </div>
          </li>
<li class="">
            <div class="layui-menu-body-title">
              <a href="layedit.html">
                <span>富文本编辑器 </span>
                <span class="layui-font-12 layui-font-gray">layedit</span>
              </a>
            </div>
          </li>
        </ul>
      </li>
    </ul>
  </div>
  <div class="layui-hide-v"> - </div>
</div>

  
  <div class="layui-col-md9 site-content">
    <h1 class="site-h1"><i class="layui-icon layui-icon-slider"></i> 滑块文档 - layui.slider</h1>
    <blockquote class="layui-elem-quote layui-text">
      作为一个拖拽式的交互性组件，滑块往往能给产品带来更好的操作体验。layui 深以为然，slider 模块包含了你能想到的大部分功能，尽管它可以作为一个独立的个体，但很多时候它往往会出现 form 元素中，想象一下吧。
    </blockquote>
    <blockquote class="layui-elem-quote">
      模块加载名称：<em>slider</em>
      <p class="layui-text" style="margin-top: 10px;">注意：<em>slider 为 layui 2.4.0 新增模块</em></p>
    </blockquote>
    
    
    
    <fieldset class="layui-elem-field layui-field-title site-title">
      <legend><a name="use">使用</a></legend>
    </fieldset>
    <div class="site-text">
      <p>通过对 slider 模块的使用，你可以在页面构建出可拖动的滑动元素，如下是一个最基本的用法：</p>
      <pre class="layui-code" lay-title="小例子">
&lt;!DOCTYPE html>
&lt;html>
&lt;head>
  &lt;meta charset="utf-8">
  &lt;title>滑块&lt;/title>
  &lt;link rel="stylesheet" href="../src/css/layui.css">
&lt;/head>
&lt;body>
&lt;div id="slideTest1">&lt;/div>
&lt;script src="../src/layui.js">&lt;/script>
&lt;script>
layui.use('slider', function(){
  var slider = layui.slider;
  
  //渲染
  slider.render({
    elem: '#slideTest1'  //绑定元素
  });
});
&lt;/script>
&lt;/body>
&lt;/html>
      </pre>
    </div>
 
    <fieldset class="layui-elem-field layui-field-title site-title">
      <legend><a name="options">基础参数</a></legend>
    </fieldset>
    <div class="site-text">
      <p>slider 组件支持以下参数</p>
      <table class="layui-table">
        <colgroup>
          <col width="120">
          <col>
          <col>
          <col width="100">
        </colgroup>
        <thead>
          <tr>
            <th>参数选项</th>
            <th>说明</th>
            <th>类型</th>
            <th>默认值</th>
          </tr> 
        </thead>
        <tbody>
          <tr>
            <td>elem</td>
            <td>指向容器选择器</td>
            <td>string/object</td>
            <td>-</td>
          </tr>
          <tr>
            <td>type</td>
            <td>滑块类型，可选值有：<em>default</em>（水平滑块）、<em>vertical</em>（垂直滑块）</td>
            <td>string</td>
            <td>default</td>
          </tr>
          <tr>
            <td>min</td>
            <td>滑动条最小值，正整数，默认为 0 </td>
            <td>number</td>
            <td>0</td>
          </tr>
          <tr>
            <td>max</td>
            <td>滑动条最大值</td>
            <td>number</td>
            <td>100</td>
          </tr>
          <tr>
            <td>range</td>
            <td>是否开启滑块的范围拖拽，若设为 true，则滑块将出现两个可拖拽的环</td>
            <td>boolean</td>
            <td>false</td>
          </tr>
          <tr>
            <td>value</td>
            <td>滑块初始值，默认为数字，若开启了滑块为范围拖拽（即 range: true），则需赋值数组，异表示开始和结尾的区间，如：value: [30, 60]</td>
            <td>number/Array</td>
            <td>0</td>
          </tr>
          <tr>
            <td>step</td>
            <td>拖动的步长</td>
            <td>number</td>
            <td>1</td>
          </tr>
          <tr>
            <td>showstep</td>
            <td>是否显示间断点</td>
            <td>boolean</td>
            <td>false</td>
          </tr>
          <tr>
            <td>tips</td>
            <td>是否显示文字提示</td>
            <td>boolean</td>
            <td>true</td>
          </tr>
          <tr>
            <td>input</td>
            <td>
              是否显示输入框（注意：若 range 参数为 true 则强制无效）
              <br>点击输入框的上下按钮，以及输入任意数字后回车或失去焦点，均可动态改变滑块
            </td>
            <td>boolean</td>
            <td>false</td>
          </tr>
          <tr>
            <td>height</td>
            <td>滑动条高度，需配合 type:"vertical" 参数使用</td>
            <td>number</td>
            <td>200</td>
          </tr>
          <tr>
            <td>disabled</td>
            <td>是否将滑块禁用拖拽</td>
            <td>boolean</td>
            <td>false</td>
          </tr>
          <tr>
            <td>theme</td>
            <td>主题颜色，以便用在不同的主题风格下</td>
            <td>string</td>
            <td>#009688</td>
          </tr>          
        </tbody>
      </table>
    </div> 
    <fieldset class="layui-elem-field layui-field-title site-title">
      <legend><a name="ontips">自定义提示文本</a></legend>
    </fieldset>
    <div class="site-text">
      <p>当鼠标放在圆点和滑块拖拽时均会触发提示层。其默认显示的文本是它的对应数值，你也可以自定义提示内容：</p>
      <pre class="layui-code" lay-title="例子">
slider.render({
  elem: '#slideTest1'
  ,setTips: function(value){ //自定义提示文本
    return value + '%';
  }
});
      </pre>
    </div>
    <fieldset class="layui-elem-field layui-field-title site-title">
      <legend><a name="change">数值改变的回调</a></legend>
    </fieldset>
    <div class="site-text">
      <p>在滑块数值被改变时触发。该回调非常重要，可动态获得滑块当前的数值。你可以将得到的数值，赋值给隐藏域，或者进行一些其它操作。</p>
      <pre class="layui-code" lay-title="小例子">
//当滑块为普通模式，回调返回的 value 是一个数值
slider.render({
  elem: '#slideTest1'
  ,change: function(value){
    console.log(value) //动态获取滑块数值
    //do something
  }
});
 
//当滑块为范围模式，回调返回的 value 是一个数组，包含开始和结尾
slider.render({
  elem: '#slideTest1'
  ,range: true
  ,change: function(value){
    console.log(value[0]) //得到开始值
    console.log(value[1]) //得到结尾值
    //do something
  }
});
      </pre>
    </div>
    
    <fieldset class="layui-elem-field layui-field-title site-title">
      <legend><a name="method">实例方法</a></legend>
    </fieldset>
    <div class="site-text">
      <p>
        执行 slider 实例时，会返回一个当前实例的对象，里面包含针对当前实例的方法和属性。
        <br>语法：<em>var ins1 = slider.render(options);</em>
      </p>
      <pre class="layui-code" lay-title="实例方法和属性">
var ins1 = slider.render(options); //获得实例对象
 
ins1.config //获得当前实例的配置项
ins1.setValue(nums); //动态给滑块赋值
      </pre>
    </div>
    
    <fieldset class="layui-elem-field layui-field-title site-title">
      <legend><a name="setValue">动态改变滑块数值</a></legend>
    </fieldset>
    <div class="site-text">
      <p>你可以通过外部方法动态改变滑块数值，如：</p>
      <pre class="layui-code">
var ins1 = slider.render({
  elem: '#test1'
  //…
});      
 
//改变指定滑块实例的数值
ins1.setValue(20)
 
//若滑块开启了范围（range: true）
ins1.setValue(20, 0) //设置开始值
ins1.setValue(60, 1) //设置结尾值
      </pre>
    </div>
    
    <fieldset class="layui-elem-field layui-field-title site-title">
      <legend><a name="docend">结语</a></legend>
    </fieldset>
    <div class="site-text">
      <p>layui.slider 可以大幅度提升你 Web 应用中的很多操作体验，可尽情发挥。</p>
    </div>
 
    <div class="layui-elem-quote">
  <p>layui - 在每一个细节中，用心与你沟通</p>
</div>
    
  </div>
</div>
<script>
window.global = {
  pageType: 'doc'
  ,preview: function(){
    var preview = document.getElementById('LAY_preview');
    return preview ? preview.innerHTML : '';
  }()
};
</script>
<script async="" src="../../pagead/js/adsbygoogle.js"></script>
<div class="site-tree-mobile layui-hide">
  <i class="layui-icon layui-icon-spread-left"></i>
</div>
<div class="site-mobile-shade"></div>
 
<script src="../../layui/dist/layui.js" ></script>
<script>
layui.config({
  base: '../../static/lay/modules/layui/'
  ,version: '1632428048355'
}).use('global');
</script>
 </body>
</html>